var BASE_HTTP_URL: string = ""
var DEV: boolean = false
const httpUtils = {
    BASE_HTTP_URL,
    DEV,
    Method: {
        GET: "GET",
        POST: "POST",
        PUT: "PUT",
        DELETE: "DELETE"
    },
    request(url: string, data: RequestInit, invoke?: Function, error?: Function): any {
        fetch(BASE_HTTP_URL + url, data)
            .then(response => {
                if (response.ok != true || response.status != 200) {
                    if(DEV){
                        console.log("request error ! status code: " + response.status); 
                    }
                    return;
                }
                return response.json()
            }).then(data => {
                if (invoke != null) {
                    invoke(data)
                }
            })
            .catch((err) => {
                if (error != null) {
                    error(err)
                }
            })
    },
    getImage(url: string,invoke: Function, error?: Function){
        fetch(BASE_HTTP_URL+url, {
            method: 'GET'
        }).then(response=>{
            if (response.ok != true || response.status != 200) {
                if(DEV){
                    console.log("request error ! status code: " + response.status); 
                }
                return;
            }
            return response.blob()
        }).then(data=>{
            invoke(data)
        }).catch(err=>{
            if(error!=null){
                error(err)
            }
        })
    }
}

export {httpUtils}